import Vue from 'vue'
import VueRouter from 'vue-router'
import { createRouter, createWebHistory } from 'vue-router'
import Home from '@/views/Home.vue'
import Contact from '@/views/contact/Contact.vue'
import CulturalActivities from '@/views/CulturalActivities.vue'
import Login from '@/Login.vue'
import Register from '@/views/Register.vue'
import TrafficGuide from '@/views/strategy/TrafficGuide.vue'
import AccommodationGuide from '@/views/strategy/AccommodationGuide.vue'
import TipsGuide from '@/views/strategy/TipsGuide.vue'
import Attractions from '@/views/attraction/Attractions.vue'
import AttractionDetail from '@/views/attraction/AttractionDetail.vue'
import Routes from '@/views/Routes.vue'
import Guide from '@/views/Guide.vue'
import Food from '@/views/Food.vue'
import Events from '@/views/events/Events.vue'
import EventDetail from '@/views/events/EventDetail.vue'
import FAQ from '@/views/FAQ.vue'
import Profile from '@/views/Profile.vue'
// admin
import AdminLogin from '@/views/Admin/AdminLogin.vue'
import AdminMain from '@/views/Admin/AdminMain.vue'
import attractionManage from '@/views/Admin/attractionManage/AttractionManage.vue'
import foodManage from '@/views/Admin/foodManage/FoodManage.vue'
import Main from '@/Main.vue'

Vue.use(VueRouter)


const router = new VueRouter({
	routes: [{
			path: '/',
			name: 'Home',
			component: Home,
			meta: { keepAlive: true }
		}, {
			path: '/adminLogin',
			name: 'AdminLogin',
			component: AdminLogin
		}, {
			path: '/adminMain',
			name: 'AdminMain',
			component: AdminMain,
			children: [{
				path: '/attractionManage',
				name: attractionManage,
				component: attractionManage // 添加组件配置
			},
			{
				path: '/foodManage',
				name: foodManage,
				component: foodManage // 添加组件配置
			}]
		},
		{
			path: '/attractions',
			name: 'Attractions',
			component: Attractions,
			meta: { keepAlive: true }
		},
		{
			path: '/attractions/:id',
			name: 'AttractionDetail',
			component: AttractionDetail,
			props: true
		},
		{
			path: '/contact',
			name: 'Contact',
			component: Contact,
			meta: { keepAlive: true }
		},
		{
			path: '/cultural-activities',
			name: 'CulturalActivities',
			component: CulturalActivities,
			meta: { keepAlive: true }
		},
		{
			path: '/events',
			name: 'Events',
			component: Events,
			meta: { keepAlive: true }
		},
		{
			path: '/events/:id',
			name: 'EventDetail',
			component: EventDetail,
			props: true // 启用路由参数传递给组件
		},
		{
			path: '/faq',
			name: 'FAQ',
			component: FAQ,
			meta: { keepAlive: true }
		},
		{
			path: '/food',
			name: 'Food',
			component: Food,
			meta: { keepAlive: true } // 添加keepAlive标识
		},
		{
			path: '/guide',
			name: 'Guide',
			component: Guide,
			children: [{
					path: 'traffic',
					name: 'TrafficGuide',
					component: TrafficGuide,
					meta: { keepAlive: true }
				},
				{
					path: 'accommodation',
					name: 'AccommodationGuide',
					component: AccommodationGuide,
					meta: { keepAlive: true }
				},
				{
					path: 'tips',
					name: 'TipsGuide',
					component: TipsGuide,
					meta: { keepAlive: true }
				}
			]
		},
		{
			path: '/login',
			name: 'Login',
			component: Login
		},
		{
			path: '/profile',
			name: 'Profile',
			component: Profile,
			meta: { keepAlive: true, requiresAuth: true }
		},
		{
			path: '/routes',
			name: 'Routes',
			component: Routes,
			meta: { keepAlive: true }
		},
		{
			path: '/register',
			name: 'Register',
			component: Register
		}
	]
})

// 路由守卫 - 在每次路由跳转前执行
router.beforeEach((to, from, next) => {
	// 定义需要登录才能访问的路由名称列表
	const protectedRoutes = ['AttractionDetail', 'AccommodationGuide', 'TipsGuide', 'Profile'];
	// 检查本地存储中是否保存了账号密码
	const storedUsername = sessionStorage.getItem('account');
	const storedPassword = sessionStorage.getItem('password');
	const isLoggedIn = !!storedUsername && !!storedPassword;

	// 如果目标路由在保护列表中且用户未登录
	if (protectedRoutes.includes(to.name) && !isLoggedIn) {
		// 跳转到登录页，并记录当前想要访问的路径（用于登录后重定向）
		next({
			name: 'Login',
			query: {
				redirect: to.fullPath
			}
		});
	} else {
		// 否则允许正常跳转
		next();
	}
});

export default router

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
	return originalPush.call(this, location).catch(err => err)
}

const originalReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace(location) {
	return originalReplace.call(this, location).catch(err => err)
}